//
//  GWItemsDetailViewController.m
//  GiftWanted
//
//  Created by lanou on 15/11/10.
//  Copyright © 2015年 陈文杰. All rights reserved.
//

#import "GWItemsDetailViewController.h"
#import "GWHotItemsModel.h"
#import "GWItemsCollectionViewCell.h"
#import "GWItemViewController.h"

@interface GWItemsDetailViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *mainCollectionView;
@property (nonatomic, strong) NSMutableArray *dataSourceArray;

@end

@implementation GWItemsDetailViewController
- (NSMutableArray *)dataSourceArray {
    if (!_dataSourceArray) {
        _dataSourceArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.leftButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:(UIControlStateNormal)];
    [self.leftButton addTarget:self action:@selector(handleReturnButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
    self.titleLabel.text = self.name;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 5;
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    _mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, SCREEN_HEIGHT-70) collectionViewLayout:layout];
    _mainCollectionView.backgroundColor = [UIColor clearColor];
    _mainCollectionView.delegate = self;
    _mainCollectionView.dataSource = self;
    _mainCollectionView.backgroundColor = [UIColor colorWithWhite:0.913 alpha:1.000];
    [_mainCollectionView registerClass:[GWItemsCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    [self.view addSubview:_mainCollectionView];
    [self requestData];
}

- (void)requestData {
    
    NSString *urlStr = [@"http://api.liwushuo.com/v2/item_subcategories/" stringByAppendingFormat:@"%@%@", self.id_s, @"/items?limit=20&offset=0"];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]]];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self.dataSourceArray removeAllObjects];
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        for (NSDictionary *itemDic in dict[@"data"][@"items"]) {
            GWHotItemsModel *model = [[GWHotItemsModel alloc] init];
            [model setValuesForKeysWithDictionary:itemDic];
            [self.dataSourceArray addObject:model];
        }
        [self.mainCollectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"网络请求失败");
    }];
    [operation start];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataSourceArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GWItemsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor whiteColor];
    GWHotItemsModel *model = _dataSourceArray[indexPath.item];
    [cell setConfigureModel:model];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake((SCREEN_WIDTH-15)/2, (SCREEN_WIDTH-15)/2+75);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    GWHotItemsModel *model = _dataSourceArray[indexPath.item];
    GWItemViewController *itemVC = [[GWItemViewController alloc] init];
    itemVC.id_s = model.id_s;
    [self.navigationController pushViewController:itemVC animated:YES];
}

- (void)handleReturnButtonAction {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
